<template>
  <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    status-icon
    :rules="rules"
    @submit.prevent
    label-width="120px"
    class="demo-ruleForm"
  >
    <el-row :gutter="20">
      <el-col :span="8">
        <el-form-item label="设备类型" prop="type">
          <el-select v-model="ruleForm.type" placeholder="设备类型">
            <el-option
              v-for="item in deviceOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="设备状态" prop="status">
          <el-select :disabled="true" v-model="ruleForm.status" placeholder="设备状态">
            <el-option
              v-for="item in deviceStatusOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="设备名称" prop="name">
          <el-input placeholder="设备名称" v-model="ruleForm.name" type="input" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="设备安装位置" prop="location">
          <el-input placeholder="设备安装位置" v-model="ruleForm.location" type="input" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="固件版本" prop="firmware_version">
          <el-input placeholder="固件版本" v-model="ruleForm.firmware_version" type="input" />
        </el-form-item>
      </el-col>
      <!-- <el-col :span="8">
        <el-form-item label="IP地址" prop="ip_address">
          <el-input placeholder="IP地址" v-model="ruleForm.ip_address" type="input" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="MAC地址" prop="mac_address">
          <el-input placeholder="MAC地址" v-model="ruleForm.mac_address" type="input" />
        </el-form-item>
      </el-col> -->
    </el-row>
    <el-row :gutter="20">
      <el-col :span="24">
        <el-form-item label="设备描述" prop="description">
          <el-input
            placeholder="设备描述"
            v-model="ruleForm.description"
            maxlength="100"
            :rows="5"
            type="textarea"
          />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script lang="ts" setup>
import { reactive, ref, defineExpose } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
interface IOption {
  value: string
  label: string
}
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  id: '',
  name: '',
  type: '',
  status: '',
  location: '',
  ip_address: '',
  mac_address: '',
  firmware_version: '',
  description: ''
})
const deviceOption = ref<IOption[]>([
  {
    label: '传感器',
    value: 'sensor'
  },
  {
    label: '控制器',
    value: 'controller'
  },
  {
    label: '网关',
    value: 'gateway'
  },
  {
    label: '摄像头',
    value: 'camera'
  },
  {
    label: '其他',
    value: 'other'
  }
])

const deviceStatusOption = ref<IOption[]>([
  {
    label: '在线',
    value: 'online'
  },
  {
    label: '离线',
    value: 'offline'
  },
  {
    label: '故障',
    value: 'error'
  },
  {
    label: '维护中',
    value: 'maintenance'
  }
])
const rules = reactive<FormRules<typeof ruleForm>>({
  type: [
    {
      required: true,
      message: '请输入设备类型',
      trigger: 'blur'
    }
  ],
  status: [
    {
      required: false,
      message: '请输入设备状态',
      trigger: 'blur'
    }
  ],
  name: [
    {
      required: true,
      message: '请输入名称',
      trigger: 'blur'
    }
  ]
})
const setFormData = (data: any) => {
  Object.keys(ruleForm).forEach((key) => {
    if (data[key]) {
      ruleForm[key] = data[key]
    }
  })
}

defineExpose({
  ruleForm,
  setFormData,
  ruleFormRef
})
</script>
<style lang="less" scoped></style>
